/*


*/

#include "MsgRounter.h"
#include "Game.h"

MsgRounter::MsgRounter()
{
	//ctor
}

MsgRounter::~MsgRounter()
{
	//dtor
}

void MsgRounter::ServerRounter(Message* _pMsg)
{
//cout<< "GameLoop() 2.2"<< endl;
		switch(_pMsg->m_cmd){
//				case eCmd::C2S_Test_Hello:
//				{
//					string getf =_pMsg->GetString();
//					Message MsgSend(_pMsg->m_fd, eCmd::S2C_Test_Hello);
//
//					MsgSend.AddString(u8"shijie网络启动");
//					MsgSend.AddShort(9);
//
//					Message::Send(&Game::GetInstance().GetSock()->GetMsgQunue(), &MsgSend);
//				}
//				break;
				case eCmd::C2L_Login_Login:
				{
					string user = _pMsg->GetString();
					string pwd =_pMsg->GetString();

					//cout<< "C2S_Login_Login, user:"<< user<< " pwd:"<< pwd<< endl;
					int ret = Game::GetInstance().m_Login.Verification(user, pwd);

					Message Msg1(_pMsg->m_fd, eCmd::L2C_Login_Login_Ret);
					Msg1.AddInt(ret);
					Message::Send(&Game::GetInstance().GetSock().GetMsgQunue(), &Msg1);
				}
				break;
				case eCmd::SYS_Check_Answer:
				{
					int retKey = _pMsg->GetInt();
					bool isServer = _pMsg->GetBool();

					//Debug::Log("Recv SYS_Check_Answer");
					//Debug::Log("retKey:", retKey);
					//Debug::Log("isServer:", isServer);

					// 返回key不对， 不是我们程序 关闭链接
					if(retKey != 8259)
					{
						Debug::Warn("Not our process, shutdown connect.");
						Game::GetInstance().GetSock().ShutDownAConnect(_pMsg->m_fd);
						break;
					}

					Client* pClient = Game::GetInstance().GetSock().GetClient(_pMsg->m_fd);
					if(pClient == nullptr)
					{
						Debug::Error("Not found Client in socket");
						return;
					}

					// 链接的socket客户端通过验证
					pClient->m_isPassCheck = true;
					if(isServer)
					{// 是服务器程序
						Game::GetInstance().m_Login.AddServerInfo(pClient);

						// 请求服务器信息
						Message Msg(_pMsg->m_fd, eCmd::L2W_Login_Info);
						Message::Send(&Game::GetInstance().GetSock().GetMsgQunue(), &Msg);
					}
					else
					{// 是客户端程序
						Game::GetInstance().m_Login.AddClientInfo(pClient);

						// 请求客户端信息
						Message Msg(_pMsg->m_fd, eCmd::L2C_Login_Info);
						Message::Send(&Game::GetInstance().GetSock().GetMsgQunue(), &Msg);
					}

				}
				break;
				case eCmd::C2L_Login_Info:
				{
					string version = _pMsg->GetString();
					string device = _pMsg->GetString();
					string os = _pMsg->GetString();
					string channel = _pMsg->GetString();

					ClientInfo* pInfo = Game::GetInstance().m_Login.GetClientInfo(_pMsg->m_fd);
                    if(pInfo == nullptr)
					{
						break;
					}

					pInfo->UpdateVersion(version);
					pInfo->UpdateDevice(device);
					pInfo->UpdateOs(os);
					pInfo->UpdateChannel(channel);

					//Debug::Log("Update ClientInfo, ", version, device, os, channel);

				}
				break;
				case eCmd::W2L_Login_Info:
				{
					int id = _pMsg->GetInt();
					string name = _pMsg->GetString();
					eServerState state = (eServerState)_pMsg->GetInt();
					string loginIp = _pMsg->GetString();
					int loginPort = _pMsg->GetInt();


					ServerInfo* pInfo = Game::GetInstance().m_Login.GetServerInfo(_pMsg->m_fd);
					if(pInfo == nullptr)
					{
						break;
					}

					// YU_TODO: 改成方法
					pInfo->m_id = id;
					pInfo->m_name = name;
					pInfo->m_State = state;
					pInfo->m_loginIp = loginIp;
					pInfo->m_loginPort = loginPort;
				}
				break;

				case eCmd::W2L_Login_Info_Update:
					{
						//Debug::Log(" eCmd::W2L_Login_Info_Update");
						int id = _pMsg->GetInt();
						eServerState state = (eServerState)_pMsg->GetInt();
						string loginIp = _pMsg->GetString();
						int loginPort = _pMsg->GetInt();

						ServerInfo* pInfo = Game::GetInstance().m_Login.GetServerInfo(_pMsg->m_fd);
						if(pInfo == nullptr)
						{
							break;
						}

						// YU_TODO: 改成方法
						pInfo->m_State = state;
						pInfo->m_loginIp = loginIp;
						pInfo->m_loginPort = loginPort;
					}
					break;
				case eCmd::C2L_Login_ServerInfo:
				{
					list<ServerInfo> ServerInfo_lst = Game::GetInstance().m_Login.GetServerList();
					int serverCount = ServerInfo_lst.size();

					Message Msg(_pMsg->m_fd, eCmd::L2C_Login_ServerInfo);
					Msg.AddInt(serverCount);

					for(ServerInfo Info : ServerInfo_lst)
					{
						Msg.AddInt(Info.m_id);
						Msg.AddString(Info.m_name);
						Msg.AddInt((int)Info.m_State);
						Msg.AddString(Info.m_loginIp);
						Msg.AddInt(Info.m_loginPort);
					}

					Message::Send(&Game::GetInstance().GetSock().GetMsgQunue(), &Msg);
				}
				break;
				default:;
			}

}












//// 发送消息路由入口
//void MsgRounter::SendRounter(Message* _pMsg)
//{
//	_pMsg->Encode();
//
//	if(!MsgCheck(_pMsg)){
//		return;
//	}
//
//
//	// YU_TODO: 应该是不同的 write at here。 各种if吧
//	// m_ServerSock.GetMsgQunue().PushSendMsg(_pMsg);
//}
//
//
//// 消息路由
//bool MsgRounter::MsgCheck(Message* _pMsg)
//{
//	int cmd = (int)_pMsg->m_cmd;
//	if(( cmd < MSG_CMD_MIN) && (cmd > MSG_CMD_MAX))
//	{
//		cout<< "Error. cmd beyond limit."<< endl;
//		cout<< "Error at Msg.Send(), sorry, I don't know which shall I send to."<< endl;
//		return false;
//	}
//
//	return true;
//}
